package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.adapter.DiseaseDetailsGridAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.help.StringImageToList;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.DiseaseDataDetailsProtocol;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.view.MyGridView;
import com.example.administrator.zhongyichou.R;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.preview.ImagePreviewActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/12/4.
 * 描述：大病资料详情
 */

public class DiseaseDataDetailsActivity extends BaseActivity<DiseaseDataDetailsProtocol> implements AdapterView.OnItemClickListener {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_certificates_type)
    TextView txtCertificatesType;
    @BindView(R.id.txt_certificates_type1)
    TextView txtCertificatesType1;
    @BindView(R.id.txt_id)
    TextView txtId;
    @BindView(R.id.txt_disease)
    TextView txtDisease;
    @BindView(R.id.txt_address)
    TextView txtAddress;
    @BindView(R.id.txt_hospital_name)
    TextView txtHospitalName;
    @BindView(R.id.txt_data_type)
    TextView txtDataType;
    @BindView(R.id.txt_bank_name)
    TextView txtBankName;
    @BindView(R.id.txt_bank_id)
    TextView txtBankId;
    @BindView(R.id.gridview)
    MyGridView gridview;
    private String infoID;
    private String supplyID;
    private DiseaseDetailsGridAdapter adapter;
    private List<String> list;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
    }

    @Override
    protected void responseData(DiseaseDataDetailsProtocol mGsonBaseProtocal) {
        if (mGsonBaseProtocal.getData().getSupply_info().getInfo_id() == null) {
            mLoadingPage.showEmptyPage();
            return;
        }
        txtName.setText(mGsonBaseProtocal.getData().getSupply_info().getReal_name());
        txtCertificatesType.setText(mGsonBaseProtocal.getData().getSupply_info().getIdc_type().equals("0") ? "身份证" : "出生证明");
        txtCertificatesType1.setText(mGsonBaseProtocal.getData().getSupply_info().getIdc_type().equals("0") ? "身份证号" : "出生证明编号");
        txtId.setText(mGsonBaseProtocal.getData().getSupply_info().getIdc_num());
        txtDisease.setText(mGsonBaseProtocal.getData().getSupply_info().getDisease_name());
        txtAddress.setText(mGsonBaseProtocal.getData().getSupply_info().getHospital_add());
        txtHospitalName.setText(mGsonBaseProtocal.getData().getSupply_info().getHospital_name());
        txtBankName.setText(mGsonBaseProtocal.getData().getSupply_info().getBank_name());
        if (mGsonBaseProtocal.getData().getSupply_info().getBank_card().length() == 19) {
            txtBankId.setText("尾号 " + mGsonBaseProtocal.getData().getSupply_info().getBank_card().substring(mGsonBaseProtocal.getData().getSupply_info().getBank_card().length() - 4, mGsonBaseProtocal.getData().getSupply_info().getBank_card().length()) + "(" + mGsonBaseProtocal.getData().getSupply_info().getReceive_name() + ")");
        } else {
            txtBankId.setText("尾号 " + mGsonBaseProtocal.getData().getSupply_info().getBank_card() + "(" + mGsonBaseProtocal.getData().getSupply_info().getReceive_name() + ")");
        }
        switch (mGsonBaseProtocal.getData().getSupply_info().getProve_type()) {
            case "0":
                txtDataType.setText("诊断证明");
                break;
            case "1":
                txtDataType.setText("入院病案首页");
                break;
            case "2":
                txtDataType.setText("出院小结");
                break;
            case "3":
                txtDataType.setText("病情说明书");
                break;
        }
        list = StringImageToList.toList(mGsonBaseProtocal.getData().getSupply_info().getProve_img());
        adapter = new DiseaseDetailsGridAdapter(this, list);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(this);
    }

    private void initHead() {
        headerview.setTitle("资料详情");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @Override
    protected HttpLoader initHttpLoader() {
        infoID = getIntent().getStringExtra("info_id");
        supplyID = getIntent().getStringExtra("supply_id");
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(DiseaseDataDetailsProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        map.put("info_id", infoID);
//        map.put("supply_id", supplyID);
        requestHttpLoader.setParams(NetConfig.SUPPLY_INFO, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.disease_data_details_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        List<ImageInfo> imageInfo = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageInfo info = new ImageInfo();
            info.bigImageUrl = list.get(i);
            info.thumbnailUrl = list.get(i);
            //拿到view的位置
            View imageView;
            imageView = gridview.getChildAt(position);
            info.imageViewWidth = imageView.getWidth();
            info.imageViewHeight = imageView.getHeight();
            int[] points = new int[2];
            imageView.getLocationInWindow(points);
            info.imageViewX = points[0];
            info.imageViewY = points[1] - getStatusBarHeight();
            imageInfo.add(info);
        }
        // 启动预览.
        Intent intent = new Intent(this, ImagePreviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ImagePreviewActivity.IMAGE_INFO, (Serializable) imageInfo);
        bundle.putInt(ImagePreviewActivity.CURRENT_ITEM, position);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }
}
